<?php

class CysstaticDAO extends DAO {
    private static function FToN($val)
	{
		$a = unpack("I", pack("f", $val));
		return $a[1];
    }

    private static function send($pack = "")
	{
		$ip = @gethostbyname(CYSSTATIC_HOST);
		$addr = ip2long($ip);
		if (empty($addr)) return false;
		
		$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if (!is_resource($socket))
		{
			@socket_close($socket);
			return false;
		}
		if (!@socket_connect($socket, $addr, CYSSTATIC_PORT))
		{
			@socket_close($socket);
			return false;
		}

		if (defined('TCP_NODELAY')) @socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
		else @socket_set_option($socket, SOL_TCP, 1, 1);

		if (@socket_write($socket, $pack, strlen($pack)) === false)
		{
			@socket_close($socket);
			return false;
		}

		$str = '';
		if (@socket_recv($socket, $str, 1, 0) === false)
		{
			@socket_close($socket);
			return false;
		}
		$c = unpack("C", $str);
		if (!isset($c[1]) || $c[1] !== 0)
		{
			@socket_close($socket);
			return false;
		}

		@socket_close($socket);
    }

    public static function newuser($uid = 0)
	{
		if (!$uid) return;

		self::send(pack('CCNN', 0x01, 0x00, $uid, time()));
    }

    public static function login($uid = 0, $level = 0)
	{
		if (!$uid || !$level) return;

		self::send(pack('CCNNN', 0x01, 0x01, $uid, $level, time()));
    }

    public static function money($uid = 0, $level = 0, $gross = 0, $net = 0)
	{
		if (!$uid || !$level) return;

		self::send(pack('CCNNNNN', 0x01, 0x02, $uid, $level, time(), self::FToN($gross), self::FToN($net)));
    }

    //mark标志类型（uint8），如道具买入，道具使用，新手引导
    //nid操作id
    //在prop_map库定义prop_id、mark和prop_name，mark和prop_id唯一对应一个特定操作
    public static function num($uid = 0, $level = 0, $mark = 0, $nid = 0, $num = 0)
	{
		if (!$uid || !$level) return;

		self::send(pack('CCNNNCNN', 0x01, 0x03, $uid, $level, time(), $mark, $nid, $num));
    }

}